package cn.gotom.util;

import org.apache.log4j.Logger;

import cn.gotom.coding.CodingModule;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.persist.jpa.JpaPersistModule;

public class GuiceUtils
{
	public static void main(String[] args)
	{
		GuiceUtils.instance.toString();
		GuiceUtils.getInstance();
	}

	private final static Logger log = Logger.getLogger(GuiceUtils.class);

	private String jpaUnit = "ApplicationEntityManager";

	private Injector injector;

	public final static GuiceUtils instance = new GuiceUtils();

	public final static GuiceUtils getInstance()
	{
		return instance;
	}

	private GuiceUtils()
	{
		log.info("new GuiceUtils()");
	}

	public GuiceUtils(String jpaUnit)
	{
		this.setJpaUnit(jpaUnit);
	}

	protected void createInjector()
	{
		injector = Guice.createInjector(new JpaPersistModule("ApplicationEntityManager"),new CodingModule());
		log.info("createInjector：" + injector);
	}

	public String getJpaUnit()
	{
		return jpaUnit;
	}

	public void setJpaUnit(String jpaUnit)
	{
		this.jpaUnit = jpaUnit;
	}

	public Injector getInjector()
	{
		if (injector == null)
		{
			createInjector();
		}
		return injector;
	}

	public void setInjector(Injector injector)
	{
		this.injector = injector;
	}

}
